package com.swt.testdemo.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.dao.SysLogDao;
import com.swt.testdemo.entity.SysLogEntity;
import com.swt.testdemo.exception.AgyException;
import com.swt.testdemo.form.QueryLogForm;
import com.swt.testdemo.form.UserLogChartForm;
import com.swt.testdemo.form.UserLogRateForm;
import com.swt.testdemo.service.SysLogService;
import com.swt.testdemo.util.ValidatorUtil;
import com.swt.testdemo.vo.SysLogRateVO;
import com.swt.testdemo.vo.SysLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/12 11:04
 **/
@Service("SysLogService")
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {


    @Autowired
    private SysLogDao sysLogDao;




    @Override
    public ResponseBean listLog(QueryLogForm form) {
        //参数校验
        ValidatorUtil.validateEntity(form);

        Integer page = form.getPage();
        Integer limit = form.getLimit();
        Integer userIdFilter = form.getUserIdFilter();
        String operationFilter = form.getOperationFilter();

        Page<SysLogVO> entityPage = new Page<>(page,limit);
        IPage<SysLogVO> resultPage = sysLogDao.queryLog(entityPage, userIdFilter, operationFilter);


        return new ResponseBean(200,"查询成功",resultPage);
    }

    @Override
    public ResponseBean userLogRate(UserLogRateForm form) {
        //参数校验
        ValidatorUtil.validateEntity(form);
        Integer page = form.getPage();
        Integer limit = form.getLimit();
        String rateType = form.getRateType();
        rateType = rateType.toUpperCase();

        DateTime current = DateUtil.date();
        DateTime startTime = null;

        if (UserLogRateForm.RATE_TYPE_HOUR.equals(rateType)){
            startTime = DateUtil.offsetHour(current,-1);
        }
        if (UserLogRateForm.RATE_TYPE_DAY.equals(rateType)){
            startTime = DateUtil.offsetDay(current,-1);
        }
        if (UserLogRateForm.RATE_TYPE_WEEK.equals(rateType)){
            startTime = DateUtil.offsetWeek(current,-1);
        }
        if (UserLogRateForm.RATE_TYPE_MONTH.equals(rateType)){
            startTime = DateUtil.offsetMonth(current,-1);
        }
        if (startTime ==null){
            throw new AgyException("查询参数异常，筛选类型必须是 HOUR DAY WEEK MONTH");
        }





        Page<SysLogRateVO> entityPage = new Page<>(page, limit);
        IPage<SysLogRateVO> resultPage = sysLogDao.queryLogRate(entityPage, startTime.toString());



        return new ResponseBean(200,"查询成功",resultPage);
    }

    @Override
    public ResponseBean userLogChart(UserLogChartForm form) {
        //参数校验
        ValidatorUtil.validateEntity(form);




        return null;
    }
}
